package com.xiaoshuidi.cloud.module.contract.controller.admin.approval;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.ContractApprovalUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.approval.ContractApprovalConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.approval.ContractApprovalDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.approval.ContractApprovalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 合同配置房间")
@RestController
@RequestMapping(ApiConstants.NAME + "/approval")
@Validated
public class ContractApprovalController {

    @Resource
    private ContractApprovalService approvalService;

    @PostMapping("/create-rooms")
    @Operation(summary = "创建合同配置房间")
//    @PreAuthorize("@ss.hasPermission('contract:approval:create')")
    public CommonResult<Long> createRooms(@Valid @RequestBody ContractApprovalReqVO req) {
        approvalService.createRooms(req);
        return success();
    }

    @PostMapping("/create")
    @Operation(summary = "创建审批配置项")
//    @PreAuthorize("@ss.hasPermission('contract:approval:create')")
    public CommonResult<Long> createApproval(@Valid @RequestBody ContractApprovalCreateReqVO createReqVO) {
        return success(approvalService.createApproval(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同配置房间")
//    @PreAuthorize("@ss.hasPermission('contract:approval:update')")
    public CommonResult<Boolean> updateApproval(@Valid @RequestBody ContractApprovalUpdateReqVO updateReqVO) {
        approvalService.updateApproval(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同配置房间")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('contract:approval:delete')")
    public CommonResult<Boolean> deleteApproval(@RequestParam("id") Long id) {
        approvalService.deleteApproval(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同配置房间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:approval:query')")
    public CommonResult<ContractApprovalRespVO> getApproval(@RequestParam("id") Long id) {
        ContractApprovalDO approval = approvalService.getApproval(id);
        return success(ContractApprovalConvert.INSTANCE.convert(approval));
    }

    @GetMapping("/list")
    @Operation(summary = "获得合同配置房间列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('contract:approval:query')")
    public CommonResult<List<ContractApprovalRespVO>> getApprovalList(@RequestParam("ids") Collection<Long> ids) {
        List<ContractApprovalDO> list = approvalService.getApprovalList(ids);
        return success(ContractApprovalConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getApprovalList")
    @Operation(summary = "审批流id查询绑定的公寓")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('contract:approval:query')")
    public CommonResult<List<Long>> getApprovalList(@RequestParam("id") Long id) {
        List<Long> list = approvalService.getApartmentIdsListByApprovalId(id);
        return success(list);
    }

}
